<?php

function workflow_history_list($nid,$sid){
  global $user;
  $output = '';
  
  drupal_add_css(drupal_get_path('module', 'webform_workflow') . '/js/jqgrid/themes/redmond/jquery-ui-1.8.2.custom.css');
  drupal_add_css(drupal_get_path('module', 'webform_workflow') . '/js/jqgrid/themes/ui.jqgrid.css');
  drupal_add_css(drupal_get_path('module', 'webform_workflow') . '/js/jqgrid/themes/ui.multiselect.css');
  drupal_add_js(drupal_get_path('module', 'webform_workflow') . '/js/jqgrid/js/i18n/grid.locale-en.js');
  drupal_add_js(drupal_get_path('module', 'webform_workflow') . '/js/jqgrid/js/jquery.jqGrid.min.js');
  drupal_add_js(drupal_get_path('module', 'webform_workflow') . '/js/setgrid.js');
  $output .= '<table id="list3"></table><div id="pager3"></div>sdfsdfsdfsfsd';
  
  $node = node_load($nid);
  $module_code=$node->field_form_type['und'][0]['value'];
  //$modeule_code='if';
  $workflow_unique=SYSTEM_CODE.'_'.$module_code.'_'.$nid; 
  $condition=array();
  $wf_private_process_def_id = get_process_mapping_by_workflow_unique($workflow_unique,$condition);
  //$wf_private_process_def_id = 55 ;
  $userSwimlaneArray = get_swimlanes_by_role($user);
  
  //----------------get processInstanceId---------------
  $query = db_select('workflow_instance_entity_mapping', 'em', array('fetch' => PDO::FETCH_ASSOC))
  ->fields('em', array('instance_id'))
  ->condition('em.entity_id', $sid)
  ->condition('em.wf_private_process_def_id', $wf_private_process_def_id);
  $processInstanceId = $query->execute()->fetchColumn();
  //-------------end get processInstanceId---------------
  
  
  
  $json_array = array();
  $json_array['userName'] = $user->name;
  $json_array['userSwimlaneKeys'] = implode(",", $userSwimlaneArray);
  $json_array['processInstanceId'] = $processInstanceId;
  
  
  $json = json_encode($json_array); //print_r($json);die;
  $service = WORKFLOW_SERVICE_API.'getProcessInstanceHistoryLog.ws';
  
  module_load_include('inc', 'webform_workflow','webform_workflow.service');
  $api_result = call_workflow_service_api($service,$json); //print_r($api_result);die;
  $result = json_decode($api_result['body'],true); //print_r($result);die;
  
  
  //is return success

  $return_code = $result['ResponseStatusInfo']['StatusCode'];
  if($return_code == 200){
    $header = array(
      'action' => '',
      'changed' => t('Changed By'),
      'changed_date' => t('Changed Date'),
      'comments' => t('Comments')
    );
    $rows = array();
    foreach ($result['activityNodeHistoryList'] as $list) {
      $row = array();
      $row[] = array('data' => '');
      $row[] = array('data' => $list['asignee']);
      
      $list['endTime'] = ceil($list['endTime']/1000);
      $format = 'j M Y H:i';
      $list['endTime'] = format_date($list['endTime'], 'custom',$format);
      $row[] = array('data' => $list['endTime']);
        
      if(empty($list['beforeStatusCode'])){
        $list['beforeStatusCode'] = 'N/A';
      }
      $Comments = '['.$list['beforeStatusCode'] .'>'. $list['statusCode'].']' . $list['actionComment'] ;
      
      $row[] = array('data' => $Comments);
      $rows[] = $row;
    }

    //$output .= '<table id="list3"></table><div id="pager3"></div>sdfsdfsdfsfsd';
    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'workflow_history')));
    
    
    //needed page the table

  }

  return $output;
}



//return json
function workflow_history_list_api($node,$submission){
 
  global $user;
  $return_data = array();
  $nid=$node->nid;
  $sid=$submission->sid;  
  $module_code=$node->field_form_type['und'][0]['value'];
  //$modeule_code='if';
  $workflow_unique=SYSTEM_CODE.'_'.$module_code.'_'.$nid; 
  $condition=array();
  $wf_private_process_def_id = get_process_mapping_by_workflow_unique($workflow_unique,$condition);
  //$wf_private_process_def_id = 55 ;
  $userSwimlaneArray = get_swimlanes_by_role($user);
  
  //----------------get processInstanceId---------------
  $query = db_select('workflow_instance_entity_mapping', 'em', array('fetch' => PDO::FETCH_ASSOC))
  ->fields('em', array('instance_id'))
  ->condition('em.entity_id', $sid)
  ->condition('em.wf_private_process_def_id', $wf_private_process_def_id);
  $processInstanceId = $query->execute()->fetchColumn();
  //-------------end get processInstanceId---------------
      
  $json_array = array();
  $json_array['userName'] = $user->name;
  $json_array['userSwimlaneKeys'] = implode(",", $userSwimlaneArray);
  $json_array['processInstanceId'] = $processInstanceId;  
  
  $json = json_encode($json_array); 
  $service = WORKFLOW_SERVICE_API.'getProcessInstanceHistoryLog.ws';
  
  module_load_include('inc', 'webform_workflow','webform_workflow.service');
  $api_result = call_workflow_service_api($service,$json); 
  $result = json_decode($api_result['body'],true);  
  
  //is return success
  //print_R($result);
  $return_code = $result['ResponseStatusInfo']['StatusCode'];

  if($return_code == 200){   
    foreach ($result['activityNodeHistoryList'] as $list) { 
      if(empty( $list['endTime'])){
         $list['endTime']= $list['startTime'];
      }
     
     // $list['endTime'] = ceil($list['endTime']/1000);
       $list['startTime'] = ceil($list['startTime']/1000);
      $format = 'j M Y H:i';
     // $list['endTime'] = format_date($list['endTime'], 'custom',$format);
      if(empty($list['beforeStatusCode'])){
        $list['beforeStatusCode'] = 'New';
      }
      $comments = '<span class="history_log_table_hightlight">['.$list['beforeStatusCode'] .' > '. $list['statusCode'].']</span>  ' . $list['actionComment'] ;      
      $return_data[]=array(
          'history_ID'=>'',
          'operator'=>$list['asignee'],
          'changed_time'=>$list['startTime'],
          'comment'=>$comments,
      );     
    }
  }
  
  //echo date('j M Y H:i',ceil(1361884405397/1000));
  return $return_data;
}